﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace SharpShare.Configuration {
    public class StorageProviderSection<TStorageProviderElement> : ConfigurationSection, IExtensionConfigurationSection<StorageProviderElement> where TStorageProviderElement : StorageProviderElement, new() {
        [ConfigurationProperty("", IsDefaultCollection = true)]
        public GenericElementCollection<TStorageProviderElement> StorageProviders {
            get {
                return (GenericElementCollection<TStorageProviderElement>)this[""];
            }
        }

        #region IExtensionConfigurationSection<StorageProviderElement> Members

        StorageProviderElement IExtensionConfigurationSection<StorageProviderElement>.this[string name] {
            get { return this.StorageProviders[name]; }
        }

        void IExtensionConfigurationSection<StorageProviderElement>.Remove(StorageProviderElement element) {
            this.StorageProviders.Remove(element.Name);
        }

        void IExtensionConfigurationSection<StorageProviderElement>.Add(StorageProviderElement element) {
            this.StorageProviders.Add((TStorageProviderElement)element);
        }

        #endregion

        #region IExtensionConfigurationSection

        ConfigurationElement IExtensionConfigurationSection.this[string name] {
            get {
                return this.StorageProviders[name];
            }
        }

        void IExtensionConfigurationSection.Add(ConfigurationElement element) {
            this.StorageProviders.Add((TStorageProviderElement)element);
        }

        void IExtensionConfigurationSection.Remove(ConfigurationElement element) {
            this.StorageProviders.Remove(((TStorageProviderElement)element).Name);
        }

        Type IExtensionConfigurationSection.ElementType { get { return typeof(TStorageProviderElement); } }

        #endregion

        #region IEnumerable<ConfigurationElement> Members

        public IEnumerator<ConfigurationElement> GetEnumerator() {
            return this.StorageProviders.Cast<ConfigurationElement>().GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
            return this.GetEnumerator();
        }

        #endregion
    }
}
